﻿using System;
using System.Diagnostics;

namespace GluePerformanceTesting
{
    public class PerformanceMeasure
    {
        private const int timesToRun = 100000;

        private PerformanceMeasure()
        {
            
        }

        public static PerformanceMeasure GetInstance()
        {
            return new PerformanceMeasure();
        }

        public void Run(String label, Action action)
        {
            Run(label, action, 1);
        }

        public void RunLong(String label, Action action)
        {
            Run(label, action, 10);
        }

        public void RunVeryLong(String label, Action action)
        {
            Run(label,action,100);
        }

        private void Run(string label, Action action, int divideBy)
        {
            GC.Collect();
            var timer = new Stopwatch();
            timer.Start();
            for (int i = 0; i < timesToRun / divideBy; i++)
            {
                action();
            }
            timer.Stop();
            Print(timer.ElapsedTicks * divideBy, label);
        }

        private void Print(long ticks, String label)
        {
            Console.WriteLine(String.Format("{0} | {1}ms | {2}s : {3}",
                ticks.ToString().PadLeft(10),
                (ticks / TimeSpan.TicksPerMillisecond).ToString().PadLeft(6),
                (ticks / TimeSpan.TicksPerSecond).ToString().PadLeft(2),
                label));
        }

    }

}
